<template>
	<view class="container bg-fff">
		<u-search placeholder="搜索商品" :show-action="false" v-model="keyword" @search="search" @change="change"></u-search>
		
		<view v-if="is_show">
			<view class="search" v-if="historyList.length > 0">
				<view class="search-title flex-space-between">
					<view>历史搜索</view>
					<image class="search-icon" :src="$setting.imageUrl()+'delete.png'" @click="handleHistoryDelete"></image>
				</view>
				<view class="search-list flex-wrap">
					<view class="search-item flex-center" v-for="(item,index) in historyList" :key="index" @click="jump('/pages/categoty/product/list?search='+item)">{{item}}</view>
				</view>
			</view>
			
			<view class="search">
				<view class="search-title flex-space-between">
					<view>实时热搜</view>
					<!-- <image class="search-icon" :src="$setting.imageUrl()+'delete.png'" @click="handleHistoryDelete"></image> -->
				</view>
				<view class="search-list flex-wrap">
					<view class="search-item flex-center" v-for="(item,index) in hotList" :key="index" @click="jump('/pages/categoty/product/list?search='+item)">{{item}}</view>
				</view>
			</view>
		</view>
		
		<view v-if="product_show">
			<ul>
				
				<li  icon="el-icon-search" v-for="item in product_list" style="height: 70rpx;line-height: 70rpx;" @click="choose(item)">
				<u-icon name="search"  size="28"></u-icon>
				
				<span style="margin-left: 10rpx;">{{item}}</span>
				
				<view style="height: 2rpx;background-color:#eae5e5; width: 100%;text-align: center;margin: auto;"></view>
				</li>
				
			</ul>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: "",
				historyList: [],
				hotList: [],
				is_show:true,
				product_show:false,
				product_list:['漓泉','茅台']
			}
		},
		onLoad() {
			this.getHttp();
		},
		methods: {
			handleHistoryDelete() {
				uni.showModal({
					title: '提示',
					content: '删除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							this.historyList = [];
							uni.setStorageSync('history_list', []);
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			search() {
				let is_exist = false;
				this.historyList.forEach((item,index) => {
					if(item == this.keyword) {
						is_exist = true;
					}
				})
				
				if(is_exist == false) {
					this.historyList.push(this.keyword);
					uni.setStorageSync('history_list', this.historyList);
				}
				
				this.jump('/pages/categoty/product/list?search='+this.keyword);
			},
			jump(url) {
				uni.navigateTo({
					url: url
				})
			},
			handleHistoryList() {
				this.historyList = uni.getStorageSync('history_list') || [];
			},
			getConfigData() {
				uni.$u.api.getConfigData({
					type: 'hotKey'
				}).then(res => {
					this.hotList = res.list[0].content.split(',');
				})
			},
			getHttp() {
				this.handleHistoryList();
				this.getConfigData();
			},
			
			getSearchKeywords(str){
				uni.$u.api.getSearchKeywords({
					keywords: str
				}).then(res => {
					console.log('999',res.list)
					this.product_list = res.list;
					this.is_show = false
					this.product_show = true
				})
			},
			
			change(e){
				 let str = e.replace(/\s+/g, '')
				 let length = str.length;
				if(length>=2){
					this.getSearchKeywords(str)
				}else{
					this.is_show = true
					this.product_show = false
				}
			},
			choose(e){
				this.keyword = e
				this.search()
			}
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx 30rpx;
		
		.search {
			margin-top: 40rpx;
			
			.search-title {
				font-size: 28rpx;
				color: #333333;
				
				.search-icon {
					width: 30rpx;
					height: 30rpx;
				}
			}
			
			.search-list {
				margin-top: 40rpx;
				
				.search-item {
					padding: 12rpx 34rpx;
					border-radius: 28rpx;
					background: #F6F6F6;
					font-size: 24rpx;
					color: #666666;
					margin: 0 20rpx 20rpx 0;
				}
			}
		}
		
	}
</style>
